package com.tyros.common.kit.string;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.stream.Collectors;

/**
 * @author zhangshuaiyin
 * @date 2022/1/15 22:37
 */
public class StringKit implements StringPool {

    /**
     * 下划线转驼峰
     *
     * @param str 下划线字符串
     * @return 驼峰字符串
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = LINE_PATTERN.matcher(str);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * 驼峰转下划线
     *
     * @param str 驼峰字符串
     * @return 下划线字符串
     */
    public static String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, LINE + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }

    /**
     * 不满位数，左补0操作
     *
     * @param str    目标字符串
     * @param length 要求位数
     * @param left   是否左填充 true-左补0 false-右补0
     * @return String
     */
    public static String padZero(String str, int length, boolean left) {
        if (StrUtil.isBlank(str)) {
            return "";
        }
        if (str.length() >= length) {
            return str;
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length - str.length(); i++) {
            builder.append(ZERO);
        }

        if (left) {
            return builder.append(str).toString();
        }
        return str.concat(builder.toString());
    }

    /**
     * 不满位数，左补0操作
     *
     * @param str    目标字符串
     * @param length 要求位数
     * @return String
     */
    public static String leftPadZero(String str, int length) {
        return padZero(str, length, true);
    }

    /**
     * 不满位数，右补0操作
     *
     * @param str    目标字符串
     * @param length 要求位数
     * @return String
     */
    public static String rightPadZero(String str, int length) {
        return padZero(str, length, false);
    }

    /**
     * 手机号用****号隐藏中间数字
     *
     * @param phone 手机号
     * @return 脱敏手机号
     */
    public static String paddingPhone(String phone) {
        if (StrUtil.isBlank(phone)) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 邮箱用****号隐藏前面的字母
     *
     * @param email 邮箱
     * @return 脱敏邮箱
     */
    public static String paddingEmail(String email) {
        if (StrUtil.isBlank(email)) {
            return email;
        }
        return email.replaceAll("(\\w{3})(\\w+)(\\w{3})(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4");
    }

    /**
     * 集合转字符串
     *
     * @param collection 集合对象
     * @return 字符串对象
     */
    public static String toString(Collection<?> collection) {
        if (CollectionUtils.isEmpty(collection)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        collection.forEach(item -> sb.append(item).append(","));
        sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }

    /**
     * 以","分割的int类型字符串转int集合
     *
     * @param str 字符串
     * @return int集合
     */
    public static List<Integer> stringToList(String str) {
        if (StrUtil.isBlank(str)) {
            return Collections.emptyList();
        }
        List<String> split = StrUtil.split(str, ",");
        int[] ints = split.stream().mapToInt(Integer::valueOf).toArray();
        return Arrays.stream(ints).boxed().collect(Collectors.toList());
    }
}
